

using System;
using System.Collections.Generic;
using System.Text;
using DataAccessLayer;
using EntityLayer;
using DataLayer;
using System.Data;
using InfraStructureLayer;
using System.Text.RegularExpressions;

namespace BusinessLayer
{
	public class busNews:BusinessBase
	{
		public busNews() : base()
		{
		}
		#region  Code Factory Generated Methods
		public DataTable SelectNews()
		{
			try
			{
				datNews insDatNews = new datNews();
				DbConnector insDbConnector = new DbConnector();
				return insDatNews.SelectNews(insDbConnector);
			}
			catch (SFException ex)
			{
				this.ErrorMessage = ex.Message;
			}
			return null;
		}
		public void SelectNewsByNewsId(entNews parEntNews)
		{
			try
			{
				datNews insDatNews = new datNews();
				DbConnector insDbConnector = new DbConnector();
				insDatNews.SelectNewsByNewsId(parEntNews, insDbConnector);
			}
			catch (SFException ex)
			{
				this.ErrorMessage=ex.Message;
			}
		}
		
		#endregion
        public void InsertNews(entNews parEntNews)
        {
            DbConnector insDbConnector = new DbConnector();
            try
            {

                insDbConnector.BeginTransaction();
                datNews insDatNews = new datNews();

                insDatNews.InsertNews(parEntNews, insDbConnector);
                datNewsContents insDatNewsContents = new datNewsContents();

                foreach (entNewsContents insEntNewsContents in parEntNews.NewsContentsInsert)
                {
                    insEntNewsContents.NewsId = parEntNews.NewsId;
                    
                    insDatNewsContents.InsertNewsContents(insEntNewsContents, insDbConnector);
                }
                foreach (entNewsContents insEntNewsContents in parEntNews.NewsContentsUpdate)
                {
                    insEntNewsContents.NewsId = parEntNews.NewsId;
                    insDatNewsContents.UpdateNewsContentsByNewsContentId(insEntNewsContents, insDbConnector);
                }
                foreach (entNewsContents insEntNewsContents in parEntNews.NewsContentsDelete)
                {
                    insDatNewsContents.DeleteNewsContentsByNewsContentId(insEntNewsContents, insDbConnector);
                }
                insDbConnector.CommitTransaction();
            }
            catch (SFException ex)
            {
                insDbConnector.RollbackTransaction();
                this.ErrorMessage = ex.Message;
            }
            catch (Exception ex)
            {
                insDbConnector.RollbackTransaction();
                throw new SFUnHandledException(ex);
            }
        }
        public void UpdateNewsByNewsId(entNews parEntNews)
        {
            DbConnector insDbConnector = new DbConnector();
            try
            {

                insDbConnector.BeginTransaction();
                datNews insDatNews = new datNews();
                insDatNews.UpdateNewsByNewsId(parEntNews, insDbConnector);
                datNewsContents insDatNewsContents = new datNewsContents();
                foreach (entNewsContents insEntNewsContents in parEntNews.NewsContentsInsert)
                {
                    insEntNewsContents.NewsId = parEntNews.NewsId;
                    
                    insDatNewsContents.InsertNewsContents(insEntNewsContents, insDbConnector);
                }
                foreach (entNewsContents insEntNewsContents in parEntNews.NewsContentsUpdate)
                {
                    insEntNewsContents.NewsId = parEntNews.NewsId;
                    insDatNewsContents.UpdateNewsContentsByNewsContentId(insEntNewsContents, insDbConnector);
                }
                foreach (entNewsContents insEntNewsContents in parEntNews.NewsContentsDelete)
                {
                    insDatNewsContents.DeleteNewsContentsByNewsContentId(insEntNewsContents, insDbConnector);
                }
                insDbConnector.CommitTransaction();
            }
            catch (SFException ex)
            {
                insDbConnector.RollbackTransaction();
                this.ErrorMessage = ex.Message;
            }
            catch (Exception ex)
            {
                insDbConnector.RollbackTransaction();
                throw new SFUnHandledException(ex);
            }
        }
        public void DeleteNewsByNewsId(entNews parEntNews)
        {
            DbConnector insDbConnector = new DbConnector();
            try
            {

                insDbConnector.BeginTransaction();
                datNewsContents insDatNewsContents = new datNewsContents();
                entNewsContents insEntNewsContents = new entNewsContents();
                insEntNewsContents.NewsId = parEntNews.NewsId;
                insDatNewsContents.DeleteNewsContentsByNewsId(insEntNewsContents, insDbConnector);

                datNews insDatNews = new datNews();
                insDatNews.DeleteNewsByNewsId(parEntNews, insDbConnector);


                insDbConnector.CommitTransaction();
            }
            catch (SFException ex)
            {
                insDbConnector.RollbackTransaction();
                this.ErrorMessage = ex.Message;
            }
            catch (Exception ex)
            {
                insDbConnector.RollbackTransaction();
                throw new SFUnHandledException(ex);
            }
        }

        public DataTable SelectNewsLatest()
        {
            try
            {
                datNews insDatNews = new datNews();
                DbConnector insDbConnector = new DbConnector();
                return insDatNews.SelectNewsLatest(insDbConnector);

            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public DataTable SelectNewsByLanguageId()
        {
            try
            {
                datNews insDatNews = new datNews();
                DbConnector insDbConnector = new DbConnector();
                return insDatNews.SelectNewsByLanguageId(insDbConnector);
            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public DataTable SelectNewsArchive()
        {
            try
            {
                datNews insDatNews = new datNews();
                DbConnector insDbConnector = new DbConnector();
                DataTable insDataTableNewsArchive =insDatNews.SelectNewsArchive(insDbConnector);
                insDataTableNewsArchive.Columns.Add("Value", typeof(string));
                insDataTableNewsArchive.Columns.Add("Text", typeof(string));
                foreach (DataRow dr in insDataTableNewsArchive.Rows)
                {
                    dr["Value"] = dr["NewsYear"].ToString() + (dr["NewsMonth"].ToString().Length == 1 ? "0" + dr["NewsMonth"].ToString() : dr["NewsMonth"].ToString());
                    dr["Text"] = GetErrorMessageMultilanguageString(30 + Convert.ToInt32(dr["NewsMonth"]), new object[] { }) + "," + dr["NewsYear"].ToString() + " (" + dr["NewsCount"].ToString() + ")";
                }
                return insDataTableNewsArchive;
            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public DataTable SelectNewsByLanguageIdAndYearAndMonth(entNews parEntNews)
        {
            try
            {
                datNews insDatNews = new datNews();
                DbConnector insDbConnector = new DbConnector();
                return insDatNews.SelectNewsByLanguageIdAndYearAndMonth(parEntNews,insDbConnector);
            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public DataTable SelectNewsLatestMainPage()
        {
            try
            {
                datNews insDatNews = new datNews();
                DbConnector insDbConnector = new DbConnector();
                return insDatNews.SelectNewsLatestMainPage(insDbConnector);

            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
        public DataTable SelectNewsByDateMainPage(entNews parEntNews)
        {
            try
            {
                datNews insDatNews = new datNews();
                DbConnector insDbConnector = new DbConnector();
                return insDatNews.SelectNewsByDateMainPage(parEntNews, insDbConnector);

            }
            catch (SFException ex)
            {
                this.ErrorMessage = ex.Message;
            }
            return null;
        }
	}
}